extends Node2D
@onready var init_position: Node2D = %初始点位

## 测试技能配置 - 包含技能实体和等级
@export var test_skills: Array[PandoraEntity]

## 测试装备
@export var test_items: Array[PandoraEntity]

## 测试技能等级配置 - 对应test_skills数组中的技能等级
@export var test_skill_levels: Array[int] = []
## 是否启用测试技能
@export var enable_test_skills: bool = true
## 是否启用测试技能
@export var enable_test_items: bool = true

func _ready() -> void:
	await get_tree().create_timer(0.5).timeout
	## 一百次
	RaffleManager.raffle_count = 100
	RaffleManager.rocket_count = 1
	RaffleManager.select_portal_count = 100
	RaffleManager.attack_gold_count = 100
	RaffleManager.draw_card_count = 1
	RaffleManager.kill_chicken_count = 100

	Utils.set_player_stats_value("gold", 100000)
	Utils.set_player_stats_value("level", 9)
	Utils.now_level = 4
	EventBus.subscribe("to_des", on_to_des)
	## 场景进入时也重置技能冷却
	reset_all_skill_cooldowns()
	
	## 如果启用测试技能，则初始化测试技能
	if enable_test_skills:
		_init_test_skills()
	if enable_test_items:
		_init_test_items()
func _input(event: InputEvent) -> void:
	if not enable_test_skills:
		return
		
	# 测试技能快捷键
	if event is InputEventKey and event.pressed:
		match event.keycode:
			KEY_F1:  # F1 - 重新初始化测试技能
				_init_test_skills()
			KEY_F2:  # F2 - 添加第一个测试技能
				if test_skills.size() > 0:
					var level = get_test_skill_level(0)
					add_test_skill(0, level)
			KEY_F3:  # F3 - 添加第二个测试技能
				if test_skills.size() > 1:
					var level = get_test_skill_level(1)
					add_test_skill(1, level)
			KEY_F4:  # F4 - 添加第三个测试技能
				if test_skills.size() > 2:
					var level = get_test_skill_level(2)
					add_test_skill(2, level)
			KEY_F5:  # F5 - 清空所有技能
				clear_test_skills()
			KEY_F6:  # F6 - 打印当前技能列表
				print_current_skills()
			KEY_F7:  # F7 - 打印测试技能配置
				print_test_skills_config()
	
func on_to_des(_name):
	if _name == "main_scene":
		Utils.player.global_position = init_position.global_position
		## 重置所有技能冷却
		reset_all_skill_cooldowns()

## 重置所有技能冷却
func reset_all_skill_cooldowns():
	## 获取UI中的技能按钮
	var ui = get_tree().get_first_node_in_group("ui")
	if ui:
		var detail = ui.get_node_or_null("detail")
		if detail:
			var skills = detail.skills
			if skills:
				for skill_button in skills.get_children():
					if skill_button is SkillButton:
						## 重置技能冷却
						reset_skill_cooldown(skill_button)

## 重置单个技能冷却
func reset_skill_cooldown(skill_button: SkillButton):
	skill_button.reset_cooldown()

## 计算有多少节点
func get_total_node_count_iter() -> int:
	var stack: Array[Node] = [get_tree().current_scene]
	var count := 0
	while stack:
		var n = stack.pop_back()	
		count += 1
		for child in n.get_children():
			stack.append(child)
	return count

## 计算有多少节点有_process或者_physics_process函数
func get_total_node_count_iter_has_process() -> int:
	var stack: Array[Node] = [get_tree().current_scene]
	var count := 0
	while stack:
		var n :Object = stack.pop_back()
		if n.has_method("_process") or n.has_method("_physics_process"):
			count += 1
		for child in n.get_children():
			stack.append(child)
	return count

func _init_test_items() -> void:
	for item in test_items:
		EventBus.push_event("add_item", item)

## 初始化测试技能
func _init_test_skills() -> void:
	print("初始化测试技能...")
	
	# 清空当前技能列表
	SkillManager.current_skills.clear()
	
	# 遍历测试技能数组
	for i in range(test_skills.size()):
		var test_skill = test_skills[i]
		if test_skill == null:
			continue
			
		# 获取对应等级
		var skill_level = 1
		if i < test_skill_levels.size():
			skill_level = test_skill_levels[i]
			
		# 实例化技能
		var skill_instance = test_skill.instantiate()
		
		# 设置技能等级
		skill_instance.set_integer("level", skill_level)
		
		# 获取技能名称
		var skill_name = skill_instance.get_string("name")
		
		# 更新技能信息
		SkillManager.update_skill_info(skill_instance, skill_name, skill_level)
		
		# 添加到当前技能列表
		SkillManager.current_skills.append(skill_instance)
		
		# 如果是被动技能，立即生效
		if skill_instance.get_integer("need_mp") <= 0:
			var skill_func = SkillManager.get_skill_func(skill_name, skill_level)
			skill_func.call()
		
		print("添加测试技能: ", skill_name, " 等级: ", skill_level)
	
	# 触发技能变化事件
	EventBus.push_event("player_skill_change", [SkillManager.current_skills])
	print("测试技能初始化完成，共添加 ", SkillManager.current_skills.size(), " 个技能")

## 添加单个测试技能
func add_test_skill(skill_index: int, level: int = 1) -> void:
	if skill_index < 0 or skill_index >= test_skills.size():
		print("技能索引超出范围: ", skill_index)
		return
		
	var test_skill = test_skills[skill_index]
	if test_skill == null:
		print("技能实体为空")
		return
		
	# 实例化技能
	var skill_instance = test_skill.instantiate()
	
	# 设置技能等级
	skill_instance.set_integer("level", level)
	
	# 获取技能名称
	var skill_name = skill_instance.get_string("name")
	
	# 更新技能信息
	SkillManager.update_skill_info(skill_instance, skill_name, level)
	
	# 添加到当前技能列表
	SkillManager.current_skills.append(skill_instance)
	
	# 如果是被动技能，立即生效
	if skill_instance.get_integer("need_mp") <= 0:
		var skill_func = SkillManager.get_skill_func(skill_name, level)
		skill_func.call()
	
	# 触发技能变化事件
	EventBus.push_event("player_skill_change", [SkillManager.current_skills])
	print("添加测试技能: ", skill_name, " 等级: ", level)

## 通过技能名称添加测试技能
func add_test_skill_by_name(skill_name: String, level: int = 1) -> void:
	# 在test_skills数组中查找对应技能
	for i in range(test_skills.size()):
		var test_skill = test_skills[i]
		if test_skill != null and test_skill.get_string("name") == skill_name:
			add_test_skill(i, level)
			return
	
	print("未找到技能: ", skill_name)

## 移除测试技能
func remove_test_skill(skill_name: String) -> void:
	for i in range(SkillManager.current_skills.size()):
		var skill = SkillManager.current_skills[i]
		if skill.get_string("name") == skill_name:
			SkillManager.current_skills.remove_at(i)
			EventBus.push_event("player_skill_change", [SkillManager.current_skills])
			print("移除测试技能: ", skill_name)
			break

## 清空所有测试技能
func clear_test_skills() -> void:
	SkillManager.current_skills.clear()
	EventBus.push_event("player_skill_change", [SkillManager.current_skills])
	print("清空所有测试技能")

## 打印当前技能列表
func print_current_skills() -> void:
	print("当前技能列表:")
	for skill in SkillManager.current_skills:
		print("- ", skill.get_string("name"), " 等级: ", skill.get_integer("level"))

## 打印测试技能配置
func print_test_skills_config() -> void:
	print("测试技能配置:")
	for i in range(test_skills.size()):
		var skill = test_skills[i]
		var level = 1
		if i < test_skill_levels.size():
			level = test_skill_levels[i]
		if skill != null:
			print("- ", skill.get_string("name"), " 等级: ", level)
		else:
			print("- [空技能] 等级: ", level)

## 设置测试技能等级
func set_test_skill_level(skill_index: int, level: int) -> void:
	if skill_index >= 0 and skill_index < test_skill_levels.size():
		test_skill_levels[skill_index] = level
		print("设置技能等级: 索引 ", skill_index, " 等级: ", level)
	else:
		print("技能索引超出范围: ", skill_index)

## 获取测试技能等级
func get_test_skill_level(skill_index: int) -> int:
	if skill_index >= 0 and skill_index < test_skill_levels.size():
		return test_skill_levels[skill_index]
	return 1
